<?php
/**
 * add message(post)
 */
require_once('rb.php');
R::setup('mysql:host=ftp.giemiebier.nl:3306;dbname=personify', 'school', 'admin123');

$return = array();

if ($pagetree[2] != "" && isset($pagetree[2])){
    $id = $pagetree[2];
}else{
    $id = "";
}

$message = "";
if (isset($_POST['new_message']) && $_POST['new_message'] != "") {
    $message = $_POST['new_message'];
    createMessage($id,$message);
    $return['addmessage'] = true;
}
else if (isset($_POST['new_reaction']) && $_POST['new_reaction'] != "") {
    $message = $_POST['new_reaction'];
    createMessage($id,$message);
    $return['addmessage'] = true;
}
else{
    $return['addmessage'] = false;
    $return['error'] = "Geen tekst ingevoerd.";
}

echo json_encode($return);

function createMessage($id,$message){
    $posts = R::dispense("posts");
    /*if($id != "") {
        $posts->userid = $id;
    }
    else {*/
        $posts->userid = $_SESSION['id'];
    //}
    $posts->message = $message;
    $posts->time = date('Y-m-d H:i:s');
    if(isset($_POST['postid']) && $_POST['postid'] != "") {
        $posts->postid = $_POST['postid'];
    }
    R::store($posts);
}
?>
